﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XjjXmm.CodeGenerator
{
    public class DbModel
    {
        private readonly SqlSugarScope dbContext;
        public DbModel(string connectionString, DbType dbType)
        {
            dbContext = new SqlSugarScope(new ConnectionConfig()
            {
                ConnectionString = connectionString,//连接符字串
                DbType = dbType,//数据库类型
                IsAutoCloseConnection = true //不设成true要手动close
            },
                 db =>
                 {
                     //（A）这里配置参数：所有上下文都生效 
                     // db.Aop.xxx=xxxx..
                 }
            );
        }

        internal List<TableInfo> GetTableInfoList()
        {
            return dbContext.DbMaintenance.GetTableInfoList(false)
                .Select(t =>
                    new TableInfo(this)
                    {
                        DbName = t.Name,
                        Description = t.Description,
                    }
                ).ToList();
        }

        internal List<ColumnInfo> GetColumnInfosByTableName(string tableName)
        {
            return dbContext.DbMaintenance.GetColumnInfosByTableName(tableName, false)
                .Select(t =>
                    new ColumnInfo()
                    {
                        DbColumnName = t.DbColumnName,
                        DataType = t.DataType,
                        ColumnDescription = t.ColumnDescription,
                        IsNullable = t.IsNullable,
                        IsIdentity = t.IsIdentity,
                        IsPrimarykey = t.IsPrimarykey,
                    }
                ).ToList();
        }

        private List<TableInfo>? _tableInfos;
        public List<TableInfo> TableInfos
        {
            get
            {
                if (_tableInfos == null)
                {
                    _tableInfos = GetTableInfoList();
                }

                return _tableInfos;
            }

        }

    }

    public class TableInfo
    {
        private readonly DbModel _model;
        public TableInfo(DbModel model)
        {
            this._model = model;
        }
        public string DbName
        {
            get;
            set;
        }

        //public string PropertyName
        //{
        //    get;
        //    set;
        //}

        public string Description
        {
            get;
            set;
        }

        private List<ColumnInfo>? _columnInfos;
       

        public List<ColumnInfo> ColumnInfos
        {
            get
            {
                if (_columnInfos == null)
                {
                    _columnInfos = _model.GetColumnInfosByTableName(DbName);
                }

                return _columnInfos;
            }
        }
    }

    public class ColumnInfo
    {
        public string DbColumnName
        {
            get;
            set;
        }

        //public string PropertyColumnName
        //{
        //    get;
        //    set;
        //}

        public string DataType
        {
            get;
            set;
        }

        public string ColumnDescription
        {
            get;
            set;
        }

        public bool IsNullable
        {
            get;
            set;
        }

        public bool IsIdentity
        {
            get;
            set;
        }

        public bool IsPrimarykey
        {
            get;
            set;
        }

        //public string ColunmHeader { get; set; } = "";

    }

}
